node请求资源不存在的处理 && 静态资源请求的处理(7-1)

node

请求资源不存在的处理

  • [ ] try-catch处理资源不存在时出现服务崩溃问题
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    var http = require("http"),
    url = require("url"),
    fs = require("fs");
    var server1 = http.createServer(function(request, response) {
    var urlobj = url.parse(request.url, true),
    pathname = urlobj["pathname"];
    //-->避免客户请求资源不存在而服务崩溃终止(处理facicon.ico请求不到而导致的问题)
    try {
    //->按照指定目录读取文件中内容代码(字符串格式的)
    var redfs = fs.readFileSync("." + pathname, "utf-8");
    //->服务端返回到客户端的也是字符串
    response.end(redfs);
    } catch (e) {
    response.end("request file is not find");
    }
    });
    //error:listen eacces 0.0.0.0:80-->表示80端口被占用
    server1.listen(80, function() {
    console.log("server is success,listening on 80 port!");
    });

静态资源请求的处理

(处理类似默认请求favicon.ico不存在)

  • MIME类型

    每一种资源文件都会有自己的MIME类型,浏览器会根据不同类型进行解析渲染

    • TXT —> text/plain
    • HTML —> text/html
    • CSS —> text/css
    • JS —> text/javascript
    • JSON —> application/json
    • JPG —> image/jpeg
    • PNG —> image/png
  • [ ] 一条一条处理静态资源请求(html/css/js……)告诉客户端这是什么类型的文件)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    if(pathname==="/index.html"){
    var redfs = fs.readFileSync("./index.html", "utf-8");
    response.end(redfs);
    return;
    }
    if(pathname==="/index.css"){
    redfs = fs.readFileSync("./index.css", "utf-8");
    response.end(redfs);
    return;
    }
    if(pathname==="/index.js"){
    redfs = fs.readFileSync("./index.js", "utf-8");
    response.end(redfs);
    return;
    }
  • [ ] 处理静态资源文件请求(html/css/js……)告诉客户端这是什么类型的文件—>前端路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var http = require("http"),
url = require("url"),
fs = require("fs");
var server1 = http.createServer(function(request, response) {
var urlobj = url.parse(request.url, true),
pathname = urlobj["pathname"];
//-->处理静态资源文件请求(html/css/js……)告诉客户端这是什么类型的文件===>"前端路由"
var reg = /\.(HTML|JS|CSS|JSON|TXT|ICO|JPG|GIF|PNG|BMP)/i;
if (reg.test(pathname)) {
//->获取请求文件的后缀名
var suffix = reg.exec(pathname)[1].toUpperCase();
//->根据请求文件的后缀名获取当前文件的MIME类型
var suffixMIME = "text/plain"; //默认值
switch (suffix) {
case "HTML":
suffixMIME = "text/html";
break;
case "JS":
suffixMIME = "text/javascript";
break;
case "CSS":
suffixMIME = "text/CSS";
break;
case "JSON":
suffixMIME = "application/json";
break;
case "ICO":
suffixMIME = "application/octet-stream";
break;
}
try {
var redfs = fs.readFileSync("." + pathname);//有图片请求时不能写"utf-8",否则图片不显示
//->重写响应头信息:告诉客户端的浏览器,返回的内容是什么样的MIME类型&&指定内容格式编码(防止乱码)
response.writeHead(200, { "content-type":suffixMIME+";charset=utf-8;" }); //200表示成功
response.end(redfs);
} catch (e) {
response.writeHead(404, { "content-type": "text/plain;charset=utf-8;" });
response.end("request file is not find");
}
}
});
server1.listen(80, function() {
console.log("server is success,listening on 80 port!");
});
- MIME